import 'package:flutter/gestures.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:get/get.dart';
import 'package:get/get_core/src/get_main.dart';
import 'dart:ui' as ui show BoxHeightStyle, BoxWidthStyle;

import '../controller/SelectionControls.dart';
class TextFieldPage extends StatelessWidget {
  final TextEditingController _controller = TextEditingController(text: '初始内容');
  // 创建焦点
  final FocusNode _focusNode = FocusNode();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text('TextField 全属性示例')),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(children: [
          ElevatedButton(onPressed: (){
            FocusScope.of(context).unfocus();
          }, child: Text('失去焦点1')),
          ElevatedButton(onPressed: (){
            Get.focusScope?.unfocus();
          }, child: Text('失去焦点2')),
          ElevatedButton(onPressed: (){
            FocusScope.of(context).requestFocus(_focusNode);
          }, child: Text('手动获取焦点1')),
          ElevatedButton(onPressed: (){
            Get.focusScope?.requestFocus(_focusNode);
          }, child: Text('手动获取焦点2')),
          TextField(
            // 控制器和焦点
            controller: _controller,
            focusNode: _focusNode,

            // 样式相关
            style: const TextStyle(fontSize: 16, color: Colors.black),
            textAlign: TextAlign.left,
            textAlignVertical: TextAlignVertical.center,
            cursorColor: Colors.blue,
            cursorWidth: 2,
            cursorHeight: 20,
            cursorRadius: const Radius.circular(2),

            // 行数/换行
            maxLines: 3,
            minLines: 1,
            expands: false,

            // 长按菜单控制
            enableInteractiveSelection: true,
            selectionControls: OhosSelectionControls(),

            // 输入类型与格式限制
            keyboardType: TextInputType.text,
            textInputAction: TextInputAction.done,
            inputFormatters: [LengthLimitingTextInputFormatter(100)],

            // 是否可编辑
            enabled: true,
            readOnly: false,

            // 密码模式
            obscureText: false,
            obscuringCharacter: '*',

            // 输入法相关
            autofocus: false,
            enableSuggestions: true,
            enableIMEPersonalizedLearning: true,

            // 装饰（外观）
            decoration: InputDecoration(
              labelText: '标签',
              hintText: '请输入内容',
              helperText: '提示信息',
              errorText: null,
              prefixIcon: const Icon(Icons.person),
              suffixIcon: IconButton(
                icon: const Icon(Icons.clear),
                onPressed: () {
                  _controller.clear();
                },
              ),
              border: const OutlineInputBorder(),
              focusedBorder: OutlineInputBorder(
                borderSide: BorderSide(color: Colors.green),
              ),
            ),

            // 光标行为
            showCursor: true,
            selectionHeightStyle:ui.BoxHeightStyle.tight,
            selectionWidthStyle: ui.BoxWidthStyle.tight,

            // 回调
            onChanged: (value) => print('变化: $value'),
            onSubmitted: (value) => print('提交: $value'),
            onTap: () => print('点击输入框'),
            onEditingComplete: () => print('编辑完成'),
            onTapOutside: (_) => print('点击外部'),

            // 文本方向
            textDirection: TextDirection.ltr,

            // 手势行为
            dragStartBehavior: DragStartBehavior.start,

            // 滚动行为（适用于多行）
            scrollPhysics: const BouncingScrollPhysics(),
            scrollController: ScrollController(),

            // 装饰外部设置
            buildCounter: (
                BuildContext context, {
                  required int currentLength,
                  required bool isFocused,
                  required int? maxLength,
                }) {
              return Text('$currentLength/${maxLength ?? '∞'}');
            },
          )
        ],),
      ),
    );
  }
}
